package it.unipi.di.rasip;

import it.unipi.di.rasip.gui.chat.ChatForm;
import it.unipi.di.rasip.gui.chat.ChatManager;
import it.unipi.di.rasip.gui.tabs.Tab;
import it.unipi.di.rasip.protocol.Mediator;
import it.unipi.di.rasip.protocol.RasipException;


public class Chat {
	RasipUser remoteUser;
	String myUsername;
	
	Tab tab;
	ChatForm chatForm;
	
	public Chat(RasipUser remoteUser, String myUsername) {
		this.remoteUser = remoteUser;
		this.myUsername = myUsername;
	}

	public void sendMessage(final String message) throws RasipException {
		chatForm.addSelfMessage(myUsername, message);
		new Thread () {
		public void run() {
			try {
				Mediator.getInstance().sendMessage(remoteUser, message);
				super.run();
			} catch (RasipException e) {
				e.printStackTrace();
			}
			super.run();
		}	
		
		}.start();
		
	}
	
	public void receiveMessage(String message) {
		chatForm.addRemoteMessage(remoteUser.getUsername(), message);
		ChatManager.getInstance().show();
	}

	public ChatForm getChatForm() {
		return chatForm;
	}

	public void setChatForm(ChatForm chatForm) {
		this.chatForm = chatForm;
	}

	public String getMyUsername() {
		return myUsername;
	}

	public void setMyUsername(String myUsername) {
		this.myUsername = myUsername;
	}

	public RasipUser getRemoteUser() {
		return remoteUser;
	}

	public void setRemoteUser(RasipUser remoteUser) {
		this.remoteUser = remoteUser;
	}

	public Tab getTab() {
		return tab;
	}

	public void setTab(Tab tab) {
		this.tab = tab;
	}
}
